//
//  CategoryControllerView.m
//  Job
//
//  Created by 桑赐相 on 2018/8/13.
//  Copyright © 2018年 桑赐相. All rights reserved.
//

#import "CategoryControllerView.h"
#import <TYPagerController/TYTabPagerBar.h>
#import <TYPagerController/TYPagerController.h>
#import "JobListViewController.h"
@interface CategoryControllerView ()<TYTabPagerBarDataSource,TYTabPagerBarDelegate,TYPagerControllerDataSource,TYPagerControllerDelegate>
@property (nonatomic, weak) TYTabPagerBar *tabBar;
@property (nonatomic, weak) TYPagerController *pagerController;
/**  */
@property (nonatomic,strong) NSArray *types;
@end

@implementation CategoryControllerView

-(NSArray *)types{
    if (!_types) {
        _types = @[@{@"派单":@"6"},@{@"服务员":@"15"},@{@"客服":@"2"},@{@"家教":@"18"},@{@"文员":@"14"},@{@"送餐员":@"22"},
                   @{@"快递员":@"23"},@{@"推广":@"5"}, @{@"销售":@"1"},@{@"促销":@"10"},@{@"其它":@"20"}];
    }
    return _types;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addTabPageBar];
    [self addPagerController];

}
- (void)addTabPageBar {
    TYTabPagerBar *tabBar = [[TYTabPagerBar alloc]init];
    tabBar.frame = CGRectMake(0,0, CGRectGetWidth(self.view.frame), NavBarHeight);
    tabBar.layout.barStyle = TYPagerBarStyleProgressBounceView;
    tabBar.layout.normalTextColor = HexRGB(0xdddddd);
    tabBar.backgroundColor = [UIColor clearColor];
    tabBar.layout.selectedTextColor = [UIColor whiteColor];
    tabBar.layout.progressColor =  [UIColor whiteColor];
    tabBar.dataSource = self;
    tabBar.delegate = self;
    tabBar.backgroundColor = [UIColor clearColor];
    
    [tabBar registerClass:[TYTabPagerBarCell class] forCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier]];
    self.navigationItem.titleView = tabBar;
    self.tabBar = tabBar;
    [self.tabBar reloadData];
}

- (void)addPagerController {
    TYPagerController *pagerController = [[TYPagerController alloc]init];
    pagerController.layout.prefetchItemCount = 1;
    pagerController.layout.addVisibleItemOnlyWhenScrollAnimatedEnd = YES;
    pagerController.dataSource = self;
    pagerController.delegate = self;
    pagerController.view.frame = self.view.frame;
    [self addChildViewController:pagerController];
    [self.view addSubview:pagerController.view];
    _pagerController = pagerController;
    [self.pagerController reloadData];
}


- (NSInteger)numberOfItemsInPagerTabBar {
    return self.types.count;
}

- (UICollectionViewCell<TYTabPagerBarCellProtocol> *)pagerTabBar:(TYTabPagerBar *)pagerTabBar cellForItemAtIndex:(NSInteger)index {
    UICollectionViewCell<TYTabPagerBarCellProtocol> *cell = [pagerTabBar dequeueReusableCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier] forIndex:index];
    NSDictionary *dict = self.types[index];
    cell.titleLabel.text = dict.allKeys.firstObject;
    return cell;
}

#pragma mark - TYTabPagerBarDelegate
- (NSInteger)numberOfControllersInPagerController {
    return self.types.count;
}
- (CGFloat)pagerTabBar:(TYTabPagerBar *)pagerTabBar widthForItemAtIndex:(NSInteger)index {
    NSDictionary *dict = self.types[index];
    return [pagerTabBar cellWidthForTitle:dict.allKeys.firstObject];
}

- (void)pagerTabBar:(TYTabPagerBar *)pagerTabBar didSelectItemAtIndex:(NSInteger)index {
    [_pagerController scrollToControllerAtIndex:index animate:YES];
}
- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index prefetching:(BOOL)prefetching {
    NSDictionary *dict = self.types[index];
    JobListViewController *viewCtrl = [JobListViewController new];
    viewCtrl.category_id = dict.allValues.firstObject;
    return viewCtrl;
}

#pragma mark - TYPagerControllerDelegate

- (void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex animated:(BOOL)animated {
    [_tabBar scrollToItemFromIndex:fromIndex toIndex:toIndex animate:animated];
}

-(void)pagerController:(TYPagerController *)pagerController transitionFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex progress:(CGFloat)progress {
    [_tabBar scrollToItemFromIndex:fromIndex toIndex:toIndex progress:progress];
}


@end
